﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Maticsoft.DBUtility;//请先添加引用

namespace WebManager.Manager.RelateCol
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRegion();
                BindData();
            }
        }


        private void BindRegion()
        {
            LYQ.BLL.S_Group groupbll = new LYQ.BLL.S_Group();
            Region.DataSource = groupbll.GetList("GName !='系统管理员'").Tables[0];
            Region.DataBind();

        }
        protected void Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void Doc_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/New.aspx";
            Page.Response.Redirect(url);
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Form["selectCB"] == null) return;           
            string[] sel = Request.Form["selectCB"].Split(',');
            if (sel.Length > 1)
                return;
            string [] tmp = sel[0].Split(';');
            string url = URLUtility.UrlBase + "/WebManager/manager/RelateCol/Edit.aspx?Page=" + tmp[0] + "&PriID=" + tmp[1] + "&Region=" + tmp[2];
            Page.Response.Redirect(url);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Form["selectCB"] == null) return;
            string[] sel = Request.Form["selectCB"].Split(',');
            LYQ.BLL.RelateImage bllImage = new LYQ.BLL.RelateImage();
            LYQ.BLL.RelatedColumn bllCol = new LYQ.BLL.RelatedColumn();
            foreach (string d in sel)
            {
                string [] tmp = d.Split(';');
                bllCol.Delete(tmp[0], tmp[1], tmp[2]);
                bllImage.Delete(tmp[0], tmp[1], tmp[2]);
            }

            BindData();

            //string url = URLUtility.UrlBase + "/WebManager/manager/RelateCol/List.aspx";
            //Page.Response.Redirect(url);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }   
        private void BindData()
        {
            LYQ.BLL.RelatedColumn bll = new LYQ.BLL.RelatedColumn();
            string sql = "SELECT [RelatedColumn].SID, [PriColumnID],OName,[Page],[RelatedColumn].[Description],[RelatedColumn].[Region] FROM [RelatedColumn] INNER JOIN [SysObj] ON [RelatedColumn].[PriColumnID] = [SysObj].[OID] "
                       + "WHERE [RelatedColumn].[Region]='{0}'and {1} order by [PriColumnID],[RelatedColumn].SID";
            sql = string.Format(sql, Region.SelectedValue, SelPage.SelectedValue);

            GridView1.DataSource = DbHelperSQL.Query(sql).Tables[0];
            GridView1.DataBind();
            
        }

        protected void SetDefault_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/manager/RelateCol/SetDefaultAD.aspx";
            Page.Response.Redirect(url);
        }
    }
}
